package com.yuven.appframework.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.yuven.baselib.utils.Logger;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.UUID;

public class InternetUtils {
    public static final int NETWORK_TYPE_NR = 20;

    public static final String NETWORN_2G = "2";

    public static final String NETWORN_3G = "3";

    public static final String NETWORN_4G = "4";

    public static final String NETWORN_5G = "6";

    public static final String NETWORN_MOBILE = "5";

    public static final String NETWORN_NONE = "0";

    public static final String NETWORN_WIFI = "1";

    public static String getDeviceId(Context paramContext) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("a");
        try {
            TelephonyManager telephonyManager = (TelephonyManager) paramContext.getSystemService("phone");
            String str2 = telephonyManager.getDeviceId();
            if (!str2.isEmpty()) {
                stringBuilder.append("imei");
                stringBuilder.append(str2);
                Log.e("getDeviceId : ", stringBuilder.toString());
                return stringBuilder.toString();
            }
            String str1 = telephonyManager.getSimSerialNumber();
            if (!str1.isEmpty()) {
                stringBuilder.append("sn");
                stringBuilder.append(str1);
                Log.e("getDeviceId : ", stringBuilder.toString());
                return stringBuilder.toString();
            }
            str1 = getUUID();
            if (!str1.isEmpty()) {
                stringBuilder.append("id");
                stringBuilder.append(str1);
                Log.e("getDeviceId : ", stringBuilder.toString());
                return stringBuilder.toString();
            }
        } catch (SecurityException securityException) {
            securityException.printStackTrace();
            stringBuilder.append("id");
            stringBuilder.append(getUUID());
        }
        Log.e("getDeviceId : ", stringBuilder.toString());
        return stringBuilder.toString();
    }

    public static String getDeviceManufacturer() {
        return Build.MANUFACTURER;
    }

    public static String getLocalIpAddress() {
        try {
            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
            while (enumeration.hasMoreElements()) {
                Enumeration<InetAddress> enumeration1 = ((NetworkInterface) enumeration.nextElement()).getInetAddresses();
                while (enumeration1.hasMoreElements()) {
                    InetAddress inetAddress = enumeration1.nextElement();
                    if (!inetAddress.isLoopbackAddress())
                        return inetAddress.getHostAddress();
                }
            }
        } catch (SocketException socketException) {
            Logger.i("IpAddress", socketException.toString());
        }
        return null;
    }

    public static String getNetworkState(Context paramContext) {
        ConnectivityManager connectivityManager = (ConnectivityManager) paramContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null)
            return "0";
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            if (!networkInfo.isAvailable())
                return "0";
            NetworkInfo networkInfo1 = connectivityManager.getNetworkInfo(1);
            if (networkInfo1 != null) {
                NetworkInfo.State state = networkInfo1.getState();
                if (state != null && (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING))
                    return "1";
            }
            networkInfo1 = connectivityManager.getNetworkInfo(0);
            if (networkInfo1 != null) {
                NetworkInfo.State state = networkInfo1.getState();
                String str = networkInfo1.getSubtypeName();
                if (state != null && (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING)) {
                    int i = networkInfo.getSubtype();
                    if (i != 20) {
                        switch (i) {
                            default:
                                return (!str.equalsIgnoreCase("TD-SCDMA") && !str.equalsIgnoreCase("WCDMA")) ? (str.equalsIgnoreCase("CDMA2000") ? "3" : "5") : "3";
                            case 13:
                                return "4";
                            case 3:
                            case 5:
                            case 6:
                            case 8:
                            case 9:
                            case 10:
                            case 12:
                            case 14:
                            case 15:
                                return "3";
                            case 1:
                            case 2:
                            case 4:
                            case 7:
                            case 11:
                                break;
                        }
                        return "2";
                    }
                    return "6";
                }
            }
            return "0";
        }
        return "0";
    }

    public static String getPhoneBrand() {
        return Build.BRAND;
    }

    public static String getPhoneModel() {
        return Build.MODEL;
    }

    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    public static String getVerName(Context paramContext) {
        try {
            return (paramContext.getPackageManager().getPackageInfo(paramContext.getPackageName(), 0)).versionName;
        } catch (android.content.pm.PackageManager.NameNotFoundException nameNotFoundException) {
            Logger.i(nameNotFoundException.getMessage());
            return "";
        }
    }

    public static String getWiFiAddress(Context paramContext) {
        try {
            return int2ip(((WifiManager) paramContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE)).getConnectionInfo().getIpAddress());
        } catch (Exception exception) {
            return null;
        }
    }

    public static String int2ip(int paramInt) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(paramInt & 0xFF);
        stringBuilder.append(".");
        stringBuilder.append(paramInt >> 8 & 0xFF);
        stringBuilder.append(".");
        stringBuilder.append(paramInt >> 16 & 0xFF);
        stringBuilder.append(".");
        stringBuilder.append(paramInt >> 24 & 0xFF);
        return stringBuilder.toString();
    }

    public static boolean isNetConnect(Context paramContext) {
        NetworkInfo networkInfo = ((ConnectivityManager) paramContext.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isAvailable();
    }
}
